package cliente;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import cursor.CursorController;
import factory.FabricaDeFrame;
import skin.LookAndFeelSelector;

import java.awt.Toolkit;

public class FrmCadastrarCliente extends JFrame {

	private JPanel contentPane;
	private JLabel lblNome;
	private JLabel lblTelefone;
	private JLabel lblEmail;
	private JLabel lblDtNascimento;
	private JLabel lblSexo;
	private JLabel lblCelular;
	private JFormattedTextField txtTelefone;
	private JTextField txtNome;
	private JTextField txtEmail;
	private JFormattedTextField txtDtNascimento;
	private JFormattedTextField txtCelular;
	private ButtonGroup grupoRadioButton;
	private JRadioButton rdbtnMasculino;
	private JRadioButton rdbtnFeminino;
	private JButton btnSalvar;
	private JButton btnLimpar;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LookAndFeelSelector.lookAndFeelNimbus();
					FrmCadastrarCliente frame = (FrmCadastrarCliente) FabricaDeFrame.getInstanceCadastrarCliente(); 
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public FrmCadastrarCliente() {
		setTitle("CADASTRO DE CLIENTES");
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Leo Kodama\\Documents\\Eclipse Projects\\TesteparaProjeto\\src\\Repositorio\\icon.png"));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 468, 309);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblNome = new JLabel("Nome: ");
		lblNome.setBounds(6, 28, 46, 14);
		contentPane.add(lblNome);
		
		txtNome = new JTextField();
		txtNome.setBounds(133, 25, 218, 26);
		contentPane.add(txtNome);
		txtNome.setColumns(10);
		
		lblTelefone = new JLabel("Telefone: ");
		lblTelefone.setBounds(6, 63, 73, 14);
		contentPane.add(lblTelefone);
		
		MaskFormatter mkTel;
		try {
			mkTel = new MaskFormatter("(##) ####-####");
			txtTelefone = new JFormattedTextField(mkTel);
			txtTelefone.setBounds(133, 60, 106, 25);
			contentPane.add(txtTelefone);
			txtTelefone.setColumns(10);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		lblCelular = new JLabel("Celular:");
		lblCelular.setBounds(6, 100, 46, 14);
		contentPane.add(lblCelular);
		
		MaskFormatter mkcel;
		try {
			mkcel = new MaskFormatter("(##) #####-####");
			txtCelular = new JFormattedTextField(mkcel);
			txtCelular.setBounds(133, 97, 106, 25);
			contentPane.add(txtCelular);
			txtCelular.setColumns(10);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(6, 141, 46, 14);
		contentPane.add(lblEmail);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(133, 138, 224, 26);
		contentPane.add(txtEmail);
		txtEmail.setColumns(10);
		
		lblDtNascimento = new JLabel("Data de Nascimento:");
		lblDtNascimento.setBounds(6, 184, 128, 14);
		contentPane.add(lblDtNascimento);
		
		MaskFormatter mkdt;
		try {
			mkdt = new MaskFormatter("##/##/####");
			txtDtNascimento = new JFormattedTextField(mkdt);
			txtDtNascimento.setBounds(133, 181, 73, 25);
			contentPane.add(txtDtNascimento);
			txtDtNascimento.setColumns(10);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		lblSexo = new JLabel("Sexo:");
		lblSexo.setBounds(276, 184, 46, 14);
		contentPane.add(lblSexo);
		
		rdbtnMasculino = new JRadioButton("M");
		rdbtnMasculino.setBounds(276, 197, 46, 25);
		contentPane.add(rdbtnMasculino);
		
		rdbtnFeminino = new JRadioButton("F");
		rdbtnFeminino.setBounds(324, 198, 46, 25);
		contentPane.add(rdbtnFeminino);
		
		grupoRadioButton = new ButtonGroup();
		grupoRadioButton.add(rdbtnMasculino);
		grupoRadioButton.add(rdbtnFeminino);
		
		btnSalvar = new JButton("Salvar");
		btnSalvar.setBounds(334, 239, 89, 23);
		contentPane.add(btnSalvar);
		ActionListener salvar = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				salvar();	
			}
		};
		salvar = CursorController.createListener(this, salvar);
		btnSalvar.addActionListener(salvar);
		
		btnLimpar = new JButton("Limpar");
		btnLimpar.setBounds(235, 239, 89, 23);
		contentPane.add(btnLimpar);
		ActionListener limpar = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				limpar();
			}
		};
		limpar = CursorController.createListener(this, limpar);
		btnLimpar.addActionListener(limpar);
	}

	public void salvar(){
		CtrlCadastrarCliente ctrl = new CtrlCadastrarCliente();
		ctrl.addCliente(txtNome, txtTelefone, txtCelular, txtEmail, txtDtNascimento, rdbtnMasculino, rdbtnFeminino);
		JOptionPane.showMessageDialog(null, "Cliente Salvo com Sucesso!");
		limpar();
	}
	
	public void limpar(){
		txtNome.setText("");
		txtTelefone.setText("");
		txtCelular.setText("");
		txtEmail.setText("");
		txtDtNascimento.setText("");
	}
}
